/*
 * 上海远境文化传媒有限公司 版权所有
 */
package helper;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 日期时间格式化类.
 */
public class DateTimeFormater {

    public static final String DEFAULT_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    public static final String DEFAULT_DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String JSON_DATETIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
    public static final String SHORT_DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_TIME_FORMAT_PATTERN = "HH:mm:ss";
    public static final String SHORT_TIME_FORMAT_PATTERN = "HH:mm";

    public static String format(Date date) {
        return format(date, DEFAULT_DATE_FORMAT_PATTERN);
    }

    public static String date(Date date) {
        return format(date, DEFAULT_DATE_FORMAT_PATTERN);
    }

    public static String dt(Date date) {
        return format(date, DEFAULT_DATETIME_FORMAT_PATTERN);
    }

    public static String time(Date date) {
        return format(date, DEFAULT_TIME_FORMAT_PATTERN);
    }
    public static String shortTime(Date date) {
        return format(date, SHORT_TIME_FORMAT_PATTERN);
    }


    public static String jsonDateTime(Date date) {
        return format(date, JSON_DATETIME_FORMAT_PATTERN);
    }

    public static String sdt(Date date) {
        return format(date, SHORT_DATETIME_FORMAT_PATTERN);
    }


    public static String format(Date date, String formatPattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat formatDate = new SimpleDateFormat(formatPattern);
        return formatDate.format(date);
    }

    public static Date convertLocalDateTime(LocalDateTime localDateTime){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

}
